package com.ruoyi.aitcommon.service;

import java.util.Calendar;

import org.springframework.stereotype.Service;

import cn.hutool.core.date.DateTime;

/**
 * 日期处理类
 * @author Administrator
 *
 */
@Service
public class DateService {
	
	/**
	 * 返回期间,"yyyy-MM" 格式,举例2022-01
	 * @param off 偏移月份,支持正数/负数
	 * @return 偏移后的期间
	 */
	public String yearMonth(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.MONTH, off);
		DateTime day=DateTime.of(cal);
		return day.toString("yyyy-MM");
	}
	
	/**
	 * 返回今天+off(day)
	 * @param off 偏移今天的天数,支持正数/负数
	 * @return 偏移后的日期,"yyyy-MM-dd" 格式字符串
	 */
	public String day(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.DATE, off);
		DateTime day=DateTime.of(cal);
		return day.toDateStr();
	}
	
	/**
	 * 返回今天+off(day) 的开始时间
	 * @param off 偏移今天的天数,支持正数/负数
	 * @return 偏移后的日期,"yyyy-MM-dd 00:00:00" 格式字符串
	 */
	public String dayBegin(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.DATE, off);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		DateTime day=DateTime.of(cal);
		return day.toString("yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 返回今天+off(day) 的结束时间
	 * @param off 偏移今天的天数,支持正数/负数
	 * @return 偏移后的日期,"yyyy-MM-dd 23:59:59" 格式字符串
	 */
	public String dayEnd(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.DATE, off);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		DateTime day=DateTime.of(cal);
		return day.toString("yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 返回今天+off(day) 的当前时间
	 * @param off 偏移今天的天数,支持正数/负数
	 * @return 偏移后的日期,"yyyy-MM-dd HH:mm:ss" 格式字符串
	 */
	public String dateTime(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.DATE, off);
		DateTime day=DateTime.of(cal);
		return day.toString("yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 返回月初,"yyyy-MM-dd" 格式字符串
	 * @param off 偏移月份,支持正数/负数,0代表本月
	 * @return 月初
	 */
	public String monthBegin(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.MONTH, off);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		DateTime day=DateTime.of(cal);
		return day.toDateStr();
	}
	
	/**
	 * 返回本月月末,"yyyy-MM-dd" 格式字符串
	 * @param off 偏移月份,支持正数/负数,0代表本月
	 * @return 月末
	 */
	public String monthEnd(int off) {
		DateTime now = DateTime.now();
		Calendar cal = now.toCalendar();
		cal.add(cal.MONTH, off+1);
		cal.set(Calendar.DAY_OF_MONTH, 0);
		DateTime day=DateTime.of(cal);
		return day.toDateStr();
	}
	
	public static void main(String[] args) {
		DateService test=new DateService();
		
		System.out.println(test.monthBegin(0));
		System.out.println(test.monthEnd(0));
		System.out.println(test.monthBegin(-1));
		System.out.println(test.monthEnd(-1));
		System.out.println(test.day(-20));
		System.out.println(test.yearMonth(-1));
		System.out.println(test.dateTime(-20));
		System.out.println(test.dayBegin(-1));
		System.out.println(test.dayEnd(-1));
	}
}
